use dataset_polls, clear

format date %dm_Y

local c2000a = date("11-07-2000", "MDY")
local c2000b = date("12-12-2000", "MDY")
local c2000c = date("11-02-2004", "MDY")
local c2016 = date("11-08-2016", "MDY")
local c2020 = date("11-03-2020", "MDY")

forvalues i = 1(1)3 {

preserve

	if `i' == 1 {
		gen x = date - `c2000b'
		local c2000c = `c2000c' - `c2000b'
		keep if year(date) < 2005
		local mytitle "(a) 2000, Democrats"
	}	
	if `i' == 2 {
		keep if year(date) > 2005 & year(date) < 2020
		gen x = date - `c2016'
		local mytitle "(c) 2016, Democrats"
	}
	if `i' == 3 {
		keep if year(date) > 2019
		gen x = date - `c2020'
		local mytitle "(e) 2020, Democrats"
	}
	
	#delimit;

	gr tw
		(sc legitimatec x if subsample == "Democrat", col(gray)
			msym(O))
		(sc legitimatec x if subsample == "Dem Cndt", col(gray) 
			msym(Oh))
		(lfit legitimatec x if regex(subsample, "Dem") & x < 0, 
			clcol(black) lwid(thick))
		(lfit legitimatec x if regex(subsample, "Dem") & x > 0 &
			date!=16396, clcol(black) lwid(thick))
		,
			legend(off)
			plotregion(style(none))
			xtitle("")
			ytitle("")
			ylab(0(0.2)1, angle(horiz))
			`myxlab'
			xline(0, lpat(dash))
			name(g`i', replace)
			title("`mytitle'")
			;
	#delimit cr
	
	gr export "_output/figure_trends_dem_`i'.pdf", replace
	
	gr close
	
restore

}
